﻿#include "MeasureView/MeasureRect.h"
#include "MeasureView/MeasureDataSet.h"
#include "MeasureView/UnitConvert.h"

namespace Qly {

MeasureRect::MeasureRect(MeasureDataSet *parent)
    :MeasureElement(parent)
{
    m_measureType = Rect;
    m_results_pix.resize(5);
    m_results_um.resize(5);
    m_keyPoints_pix.resize(2);
    m_keyPoints_um.resize(2);
    m_measureState = DS_IDLE;
    m_textTemplate = parent->m_textTemplate_MeasureRect;
}
int MeasureRect::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        m_keyPoints_pix[0] = point_pix;
        m_keyPoints_um[0] = point_um;
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        compute();
        updateText();
//        m_textBlock.moveTo(QPoint(m_results_pix[0], m_results_pix[1]));
        m_measureState = DS_STATE1;
    }
    else if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        if(m_keyPoints_pix[0] == m_keyPoints_pix[1])
        {
            m_measureState = DS_ERROR;
        }
        else
        {
            compute();
            updateText(true);

            m_measureState = DS_FINISH;
        }
    }
    return m_measureState;
}

void MeasureRect::compute()
{
    QPointF p = m_keyPoints_pix[1] - m_keyPoints_pix[0];
    m_results_pix[0] = fabs(p.x());
    m_results_pix[1] = fabs(p.y());
    m_results_pix[2] = m_results_pix[0] * m_results_pix[1]; //直径

    m_results_pix[3] = (m_keyPoints_pix[0].x() + m_keyPoints_pix[1].x()) / 2.0;
    m_results_pix[4] = (m_keyPoints_pix[0].y() + m_keyPoints_pix[1].y()) / 2.0;

    double ps = m_pParent->m_pixSize_um;
    m_results_um[0] = ps * m_results_pix[0];
    m_results_um[1] = ps * m_results_pix[1];
    m_results_um[2] = ps * ps * m_results_pix[2];

    m_results_um[3] = ps * m_results_pix[3];
    m_results_um[4] = ps * m_results_pix[4];
}

void MeasureRect::paint(QPainter &painter)
{
    if( m_measureState != DS_IDLE  || m_measureState != DS_ERROR)
    {
        painter.save();
        painter.setPen(m_pen);
        QRectF f(m_keyPoints_pix[0], m_keyPoints_pix[1]);
        painter.drawRect(f);
        m_textBlock.draw(painter);
        if(m_textMoving)
        {
            QPen pen = m_pen;
            pen.setStyle(Qt::DashDotLine);
            painter.setPen(pen);
            QPointF p = m_textBlock.anchorPoint();
            painter.drawLine(p, m_keyPoints_pix[0]);
            painter.setPen(m_pen);
        }
        painter.restore();
    }
}


int MeasureRect::mouseMove(QPointF point_pix, QPointF point_um)
{
    if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        compute();
        updateText(true);
    }
    return m_measureState;
}

void MeasureRect::updateText(bool updatePosition)
{
    QString text = m_textTemplate;
    m_pParent->m_unitConvert_MeasureRect.replaceArea(text, m_results_um[2], "@area", "@areaUnit");

    QString str3 = QString::number(m_id);
    text.replace("@id", str3, Qt::CaseSensitive);

    m_textBlock.setText(text);
    if(updatePosition)
    {
        QPointF p_pix = QPointF(m_results_pix[3], m_results_pix[4]);
        QPointF p_um = QPointF(m_results_um[3], m_results_um[4]);
        m_textBlock.moveTo(p_pix, p_um);
    }
}


}
